package com.hn.comm;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Tools {

	public static Connection getConnection(String driver, String url,
			String username, String password) throws SQLException,
			java.lang.ClassNotFoundException {
		// 第一步：加载MySQL的JDBC的驱动
		// Class.forName("com.mysql.jdbc.Driver");
		//
		// // 取得连接的url,能访问MySQL数据库的用户名,密码；studentinfo：数据库名
		// String url = "jdbc:mysql://localhost:3306/studentinfo";
		// String username = "root";
		// String password = "admin";

		Class.forName(driver);

		// 第二步：创建与MySQL数据库的连接类的实例
		Connection con = DriverManager.getConnection(url, username, password);
		return con;
	}

	public static String readFile(String fileName) {
		StringBuffer sb = new StringBuffer();
		try {
			java.io.BufferedReader reader;

			reader = new java.io.BufferedReader(
					new java.io.FileReader(fileName));

			String s = reader.readLine();

			while (s != null) {
				sb.append(s);
				sb.append("\r\n");
				s = reader.readLine();
			}
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	/**
	 * byte写入文件
	 * 
	 * @param data
	 * @param fileName
	 */
	public static void writeFile(byte[] data, String fileName) {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(fileName, true);
			fos.write(data);
			fos.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (IOException iex) {
			}
		}
	}

	public static byte[] getBytes(InputStream inputstream, int iBufferSize)
			throws IOException {
		byte[] bArr = new byte[iBufferSize];

		int iIndex = 0;
		int iLength = 0;
		int iTotalLength = 0;

		while ((iLength = inputstream.read(bArr, iIndex, bArr.length - iIndex)) != -1) {
			iIndex += iLength;
			if (iLength != -1) {
				iTotalLength += iLength;
			}
			if (iIndex >= bArr.length) {
				byte[] arrbTemp = new byte[bArr.length];
				System.arraycopy(bArr, 0, arrbTemp, 0, bArr.length);
				bArr = null;
				bArr = new byte[arrbTemp.length + iBufferSize];
				System.arraycopy(arrbTemp, 0, bArr, 0, arrbTemp.length);
				arrbTemp = null;
			}
		}
		byte[] bBack = new byte[iTotalLength];

		System.arraycopy(bArr, 0, bBack, 0, bBack.length);

		bArr = null;

		// System.gc();
		return bBack;
	}

	// 新建文件夹目录
	public static void newFolder(String folderPath) {
		String filePath = folderPath.toString();
		java.io.File myFilePath = new java.io.File(filePath);
		try {
			if (myFilePath.isDirectory()) {
				System.out.println("目录已存在!");
			} else {
				myFilePath.mkdir();
				System.out.println("新建目录成功");
			}
		} catch (Exception e) {
			System.out.println("新建目录操作出错");
			e.printStackTrace();
		}
	}

	// 级联删除文件
	public static boolean deleteFolder(String _filePath) {
		java.io.File folder = new java.io.File(_filePath);
		boolean result = false;
		try {

			String childs[] = folder.list();
			if (childs == null || childs.length <= 0) {
				if (folder.delete()) {
					result = true;
				}
			} else {
				for (int i = 0; i < childs.length; i++) {
					String childName = childs[i];
					String childPath = folder.getPath() + File.separator
							+ childName;
					File filePath = new File(childPath);
					if (filePath.exists() && filePath.isFile()) {
						if (filePath.delete()) {
							result = true;
						} else {
							result = false;
							break;
						}
					} else if (filePath.exists() && filePath.isDirectory()) {
						if (deleteFolder(filePath.toString())) {
							result = true;
						} else {
							result = false;
							break;
						}
					}
				}
			}
			folder.delete();
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}
		return result;
	}

	
	//得到所有文件夹
	public static File[] readAllFile(String path) {

		File f = null;
		f = new File(path);
		File[] files = f.listFiles(); // 得到f文件夹下面的所有文件。
		List<File> list = new ArrayList<File>();
		for (File file : files) {
			if (file.isDirectory()) {
				// 如何当前路劲是文件夹，则循环读取这个文件夹下的所有文件
				readAllFile(file.getAbsolutePath());
			} else {
				list.add(file);
			}
		}
		return files;
	}

	// GENERAL_PUNCTUATION 判断中文的“号

	// CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号

	// HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的，号

	public static boolean isChinese(char c) {

		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);

		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS

		|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS

		|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A

		|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION

		|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION

		|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {

			return true;

		}

		return false;

	}

	public static boolean isChinese(String strName) {

		char[] ch = strName.toCharArray();

		for (int i = 0; i < ch.length; i++) {

			char c = ch[i];

			if (isChinese(c) == true) {

				// System.out.println(isChinese(c));

				return true;

			} else {

				// System.out.println(isChinese(c));

				return false;

			}

		}
		return false;

	}

}
